/* 
 * File:   FractalComputeLinear.cpp
 * Author: amouro-m
 * 
 * Created on 5 juin 2010, 18:54
 */

#include "LinearCompute.h"

#include <iostream>
#include <sstream>

#include "FractalCompute.hpp"
#include "tbb/tick_count.h"
#include "Frame.h"

LinearCompute::LinearCompute(FractalPrinter* fp, ResultPanel* chrono):
_fp(fp),
_result_panel(chrono)
{}

LinearCompute::~LinearCompute() {}

void
LinearCompute::operator() (unsigned int xmin, unsigned int xmax, unsigned int ymin, unsigned int ymax)
{
    tbb::tick_count start, end;
    start = tbb::tick_count::now();
    for (unsigned int i = xmin; i < xmax; ++i)
    {
        for (unsigned int j = ymin; j < ymax; ++j)
        {
            _fp->compute(i, j);
        }
    }
    end = tbb::tick_count::now();
    tbb::tick_count::interval_t interval = (end - start);
    std::ostringstream os (std::ostringstream::out);
    os << "Block : " << interval.seconds() << " s";
    std::string string(os.str());

    _result_panel->add_item(string.c_str());
    _fp->repaint();
}

void
LinearCompute::compute(FractalPrinter* fp, ResultPanel* chrono)
{
    chrono->reset();
    tbb::tick_count start, end;
    start = tbb::tick_count::now();
    if(fp->get_fractal() != NULL)
    {
        LinearCompute fcl(fp, chrono);
        fcl(0, fp->get_width(), 0, fp->get_height());
    }
    end = tbb::tick_count::now();
    tbb::tick_count::interval_t interval = (end - start);
    std::ostringstream os (std::ostringstream::out);
    os << "Total : " << interval.seconds() << " s";
    std::string string(os.str());

    chrono->add_item(string.c_str());
}